<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input  style="width: 200px;" class="filter-item" placeholder="资源名称" v-model="listQuery.name"> </el-input>
      <el-button class="filter-item" type="primary"  icon="search" @click="getList">搜索</el-button>
      <el-button class="filter-item"  style="margin-left: 10px;"  type="primary" @click="addDetail"  v-if="isAuth('sys:dicgroupdetail:save')">添加</el-button>
    </div>
    <el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="id" width="65" type="index"></el-table-column>
      <el-table-column width="200px" align="center" prop="code" label="编码"></el-table-column>
      <el-table-column width="200px" align="center" prop="name" label="名称"></el-table-column>
      <el-table-column width="200px" align="center" prop="descp" label="备注"></el-table-column>
      <el-table-column width="50px" align="center" prop="showOrder" label="顺序"></el-table-column>
      <el-table-column align="center" label="操作" >
        <template slot-scope="scope">
        <el-button
          size="mini"
          @click="editRecord(scope.row)"  v-if="isAuth('sys:dicgroupdetail:update')">编辑</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="deleteRecord(scope.row)"  v-if="isAuth('sys:dicgroupdetail:delete')">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
   <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
  </el-pagination>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
    <el-form :model="detailForm" :rules="rules" ref="detailForm" label-width="100px">
      <el-form-item label="编码" prop="code">
        <el-input v-model="detailForm.dicGpId" v-show="false"></el-input>
        <el-input v-model="detailForm.code" placeholder="请输入编码"></el-input>
      </el-form-item>   
      <el-form-item label="名称" prop="name">
        <el-input v-model="detailForm.name" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="descp">
        <el-input v-model="detailForm.descp" placeholder="请输入描述"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="showOrder">
        <el-input v-model="detailForm.showOrder" ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel('detailForm')">取 消</el-button>
      <el-button v-if="dialogStatus=='create'" type="primary" @click="create('detailForm')">确 定</el-button>
      <el-button v-else type="primary" @click="update('detailForm')">确 定</el-button>
    </div>
  </el-dialog>
  </div>
</template>

<script>

export default {
  name: 'dicgroupdetail',
  // props: {
  //   dicGpId: {
  //     default: '1'
  //   }
  // },
  data() {
    return {
      dicGpId:'-1',
      detailForm: {
        code: undefined,
        name: undefined,
        dicGpId: undefined,
        descp: undefined,
        showOrder:undefined
      },
      rules: {
        code: [{
          required: true,
          message: '请输入编码',
          trigger: 'blur'
        }
        ],
        name: [{
          required: true,
          message: '请输入名称',
          trigger: 'blur'
        },
        {
          min: 2,
          max: 20,
          message: '长度在 2 到 20 个字符',
          trigger: 'blur'
        }
        ],
       
        descp: [{
          required: true,
          message: '请输入描述',
          trigger: 'blur'
        },
        {
          min: 3,
          max: 100,
          message: '长度在 3 到 100 个字符',
          trigger: 'blur'
        }
        ]
      },
      list: null,
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        name: undefined,
        dicGpId: this.dicGpId
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      tableKey: 0
    }
  },
  created() {
   this.getList();
    
  },
  methods: {
    getList(){ //获取列表
      this.$http({
          url: this.$http.adornUrl('/sys/dicgroupdetail/list'),
          method: 'post',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.listQuery.name,
            'dicGpId':this.dicGpId
          })
        }).then(({data}) => {
          if (data && data.success) {
            this.list = data.list
            this.totalPage = data.totalCount
          } else {
            this.list = []
            this.totalPage = 0
          }
          this.listLoading = false
        })
    },
    deleteRecord(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/dicgroupdetail/delete/'+row.dicGpDetailId),
          method: 'get',
          params: this.$http.adornParams({})
        }).then(({data}) => {    
          if(data.success){
            this.getList();
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            });
          }else{
            this.$message.error(data.msg);
          } 
        });
      });
    },
    editRecord(row){
       this.$http({
          url: this.$http.adornUrl('/sys/dicgroupdetail/info/'+row.dicGpDetailId),
          method: 'get',
          params: this.$http.adornParams({})
       }).then(({data}) => {
         if(data.success){
            this.detailForm = data.data;
            this.dialogFormVisible = true;
            this.dialogStatus = 'update';
         }else{
           this.$message.error(data.msg);
         }
        
      });
    },
    create(formName) { //保存字典项
      const set = this.$refs;
      this.detailForm.dicGpId = this.dicGpId;
      set[formName].validate(valid => {
        if (valid) {
          this.$http({
              url: this.$http.adornUrl("/sys/dicgroupdetail/save"),
              method: "post",
              data: this.$http.adornData(this.detailForm)
            }).then(({ data }) => {
              if (data && data.success) {
                this.getList();
                this.$notify({
                    title: "成功",
                    message: data.msg,
                    type: "success",
                    duration: 2000
                });
              } else {
              this.$message.error(data.msg);
            }
            this.dialogFormVisible = false;
            set[formName].resetFields();
          });
        } else {
          return false;
        }
      });
    },
    cancel(formName) {
      this.dialogFormVisible = false;
      // const set = this.$refs;
      // set[formName].resetFields();
      this.resetTemp();
    },
    update(formName) { //更新字典项
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false;
          this.$http({
             url: this.$http.adornUrl("/sys/dicgroupdetail/update"),
              method: "post",
              data: this.$http.adornData(this.detailForm)
          }).then(({data}) => {
            if(data.success){
              this.dialogFormVisible = false;
              set[formName].resetFields();
              this.getList();
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000
              });
            }else{
                this.$message.error(data.msg);
            }
            
          });
        } else {
          return false;
        }
      });
    },
    resetTemp() {
      this.detailForm = {
        code: undefined,
        name: undefined,
        descp: undefined,
        dicGpId: undefined,
        showOrder: undefined
      };
    },
     // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getList()
      },
    addDetail() { //添加按钮
      this.dialogStatus = 'create';
      this.dialogFormVisible = true;
    }
  }
}
</script>
